package com.hzcx.app.simplechat.ui.group;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.api.BaseObserver;
import com.hzcx.app.simplechat.chat.ChatUiHelper;
import com.hzcx.app.simplechat.chat.ChatUtil;
import com.hzcx.app.simplechat.model.GroupModel;
import com.hzcx.app.simplechat.ui.chat.GroupContactManageActivity;
import com.hzcx.app.simplechat.ui.chat.bean.GroupSearchBean;
import com.hzcx.app.simplechat.ui.friend.bean.GroupBean;
import com.hzcx.app.simplechat.ui.group.bean.GroupJoinBean;
import com.hzcx.app.simplechat.ui.publicui.ShareActivity;
import com.hzcx.app.simplechat.ui.publicui.bean.HxUserInfoBean;
import com.hzcx.app.simplechat.util.UserInfoUtil;
import com.hzcx.app.simplechat.util.app.AppHelper;
import com.hzcx.app.simplechat.util.glide.GlideUtils;
import com.hzcx.app.simplechat.util.toast.ToastUtils;
import com.hzcx.app.simplechat.view.imageview.RoundImageView;

public class GroupInfoFragment extends Fragment {


//    @BindView(R.id.tv_title)
    TextView tvTitle;
//    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
//    @BindView(R.id.iv_back)
    ImageView ivBack;
//    @BindView(R.id.iv_head)
    RoundImageView iconGroup;
//    @BindView(R.id.tv_to_chat)
    TextView nameGroup;
//    @BindView(R.id.tv_remark_name)
    TextView numGroup;
//    @BindView(R.id.group_count)
    TextView countGroup;
//    @BindView(R.id.group_introduce)
    TextView introduceGroup;
//    @BindView(R.id.group_add_btn)
    Button addBtn;

    public Bundle savedInstanceState;
    public GroupSearchBean groupSearchBean;

    @Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {
        return inflater.inflate(R.layout.fragment_group_info, container, false);
    }

    @Override
    @MainThread
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        this.savedInstanceState = savedInstanceState;

        tvTitle = view.findViewById(R.id.tv_title);
        tvTitle.setText("群组信息");
        ivTitleRight = view.findViewById(R.id.iv_title_right);
        ivTitleRight.setImageResource(R.mipmap.ic_icon_title_more);
        ivTitleRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), ShareActivity.class)
                        .putExtra(ShareActivity.INTENT_USER_ID, groupSearchBean.getCustom_number()));
            }
        });
        ivBack = view.findViewById(R.id.iv_back);
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((AddGroupForSearchActivity)getActivity()).finish();
            }
        });

        iconGroup = view.findViewById(R.id.iv_head);
        nameGroup = view.findViewById(R.id.tv_to_chat);
        numGroup = view.findViewById(R.id.tv_remark_name);
        countGroup = view.findViewById(R.id.group_count);
        introduceGroup = view.findViewById(R.id.group_introduce);
        addBtn = view.findViewById(R.id.group_add_btn);

        AddGroupForSearchActivity addGroupForSearchActivity = (AddGroupForSearchActivity)getActivity();
        groupSearchBean = (GroupSearchBean)addGroupForSearchActivity.getIntent().getSerializableExtra(AddGroupForSearchActivity.bean);
        GlideUtils.loadImg(addGroupForSearchActivity, groupSearchBean.getGroup_image(), iconGroup);
        nameGroup.setText(groupSearchBean.getGroup_name());
        numGroup.setText(groupSearchBean.getCustom_number());
        countGroup.setText(String.valueOf(groupSearchBean.getTotal_member()));
        if (groupSearchBean.getGroup_desc().isEmpty()) {
            introduceGroup.setText("群主很懒, 还没有群简介哦");
        } else {
            introduceGroup.setText(groupSearchBean.getGroup_desc());
        }
        addBtn.setText(groupSearchBean.isJoined() ? "聊天" : "申请加群");

        addBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (groupSearchBean.isJoined()) {
                    GroupSearchBean groupBean = groupSearchBean;
                    // 是好友，直接聊天
                    HxUserInfoBean hxUserInfoBean = new HxUserInfoBean();
                    hxUserInfoBean.setHxusername(groupBean.getHx_groupid());
                    hxUserInfoBean.setMember_id(Integer.valueOf(groupBean.getGroup_id()));
                    hxUserInfoBean.setNickname(groupBean.getGroup_name());
                    hxUserInfoBean.setAvatar(groupBean.getGroup_image());
//                hxUserInfoBean.setBy_remarks_name(data.getGroup_desc());
                    hxUserInfoBean.setBy_remarks_name("");
                    hxUserInfoBean.setRemarks_name(groupBean.getGroup_name());
                    hxUserInfoBean.setMyUserId(UserInfoUtil.getUserId());
                    hxUserInfoBean.setIs_assistant(0);
                    ChatUtil.getInstance().toGroupChatUi(getActivity(), hxUserInfoBean);
                    getActivity().finish();
                    return;
                }
                if (groupSearchBean.getJoin_mode().equals("0")) { // 直接加群
                    ((AddGroupForSearchActivity)getActivity()).applyJoinGroup("");
                } else if (groupSearchBean.getJoin_mode().equals("1") || groupSearchBean.getJoin_mode().equals("2")) {  // 回答问题
                    ((AddGroupForSearchActivity)getActivity()).selectFragment(1);
                } else {   // 类型错误
                    Log.e("加群模式错误", "" + groupSearchBean.getJoin_mode());
                }
            }
        });
    }

}